
package me.exchange.com.modules.payCloud.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import me.exchange.com.annotation.AnonymousAccess;
import me.exchange.com.modules.payCloud.utils.HttpUtil;
import me.exchange.com.modules.payCloud.utils.HttpsUtil;
import me.exchange.com.modules.payCloud.utils.TLinx2Util;
import me.exchange.com.modules.payCloud.utils.TLinxAESCoder;
import net.sf.json.JSONObject;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;
import java.util.TreeMap;

/**
 * 中品支付 签名生成
 */
@Slf4j
@RestController
@RequestMapping("/payCloud")
@Api(tags = "中品支付签名")
public class HomeController {
    //本系统给下游的密钥
    private final String OPENKEY = "ZROXL6DLtFBHEsIJuutl*%dByyTT@EnL";

    @PostMapping(value = "/buildSign")
    @AnonymousAccess
    @ApiOperation("生成签名")
    public String buildSign(String merchantNo, Map<String,String> map) {
        try {
            TreeMap<String, String> postmap = new TreeMap<String, String>();    // 请求参数的map
            postmap.put("merchantNo", merchantNo);
            TreeMap<String, String> datamap = new TreeMap<String, String>();

            //1支付5退款6撤销
            datamap.put("requestType", "1");

            // 交易类型：6京东 9微信公众号  13支付宝条码  14支付宝扫码  17微信扫码 18微信条码   19翼支付
            datamap.put("payType", map.get("payType"));
            datamap.put("amt", map.get("amt"));
            datamap.put("streamNo", map.get("streamNo"));

            datamap.put("auth_code", map.get("auth_code")); //条码必传
            datamap.put("body", map.get("body")); //条码必传

            datamap.put("divideDetail", map.get("divideDetail"));
            datamap.put("divideType", map.get("divideType"));

            /**
             * 1 data字段内容进行AES加密，再二进制转十六进制(bin2hex)
             */
            TLinx2Util.handleEncrypt(OPENKEY, datamap, postmap);

            /**
             * 2 请求参数签名 按A~z排序，串联成字符串，先进行sha1加密(小写)，再进行md5加密(小写)，得到签名
             */
            TLinx2Util.handleSign(OPENKEY, postmap);

            return postmap.toString();
        } catch (Exception e) {
            return "";
        }
    }




    @PostMapping(value = "/decodeSign")
    @AnonymousAccess
    @ApiOperation("验签")
    public String decodeSign(String rspStr) {
        try {
            JSONObject respObject = JSONObject.fromObject(rspStr);
            //验签,有data节点时才验签
            Object dataStr = respObject.get("data");
            if (!rspStr.isEmpty() && !"null".equals(String.valueOf(respObject.get("data")))) {
                if (TLinx2Util.verifySign(respObject, OPENKEY)) {    // 验签成功
                    /**
                     * 5 AES解密，并hex2bin
                     */
                    String respData = TLinxAESCoder.decrypt(dataStr.toString(), OPENKEY);

                    return respData;
                } else {
                    return "验签失败";
                }
            } else {
                return rspStr;
            }
        } catch (Exception e) {
            return e.getMessage();
        }
    }


    private String handlePost(TreeMap<String, String> postmap, String url) {
        if (url.contains("https")) {
            return HttpsUtil.httpMethodPost(url, postmap, "UTF-8");
        } else {
            return HttpUtil.httpMethodPost(url, postmap, "UTF-8");
        }
    }


}
